﻿using System;
using System.Data;
using System.Data.SqlClient;
using TestSystem.DataManipulation;
using TestSystem.DataManipulation.Helpers;

namespace TestSystem.Model.Data.Repositories
{
    public class QuotesRepository
    {
       
        public SqlDataReader GetCurrentToolBars (byte freqType, Int32 toolId)
        {
            return  SqlHelper.ExecuteAndReturnDataReader(@"GetCurrentToolBars",
                               new Param(@"ToolId", SqlDbType.Int, toolId), 
                               new Param(@"FreqType", SqlDbType.Int, freqType));
        }

        public SqlDataReader GetCurrentToolTicks (byte freqType, Int32 toolId )
        {
            return SqlHelper.ExecuteAndReturnDataReader(@"GetCurrentToolTicks",
                              new Param(@"ToolId", SqlDbType.Int, toolId),
                              new Param(@"FreqType", SqlDbType.Int, freqType));
        }

        public static void AddNew(DateTime datetime, Decimal? open, Decimal? high,Decimal? low, Decimal? close, 
                            Decimal? volume, Int16 type, Int32 toolid, Decimal? price, Int32? frequencytype)
        {
            SqlHelper.ExecuteNonQuery("AddNewQuote.sql",
                                      new Param("@DateTime", SqlDbType.DateTime, datetime),
                                      new Param("@Open", SqlDbType.Money, open ?? (object)DBNull.Value),
                                      new Param("@High", SqlDbType.Money, high ?? (object)DBNull.Value),
                                      new Param("@Low", SqlDbType.Money, low ?? (object)DBNull.Value),
                                      new Param("@Close", SqlDbType.Money, close ?? (object)DBNull.Value),
                                      new Param("@Volume", SqlDbType.Int, volume ?? (object)DBNull.Value),
                                      new Param("@Type", SqlDbType.Int, type),
                                      new Param("@ToolId", SqlDbType.Int, toolid),
                                      new Param("@Price", SqlDbType.Money, price ?? (object)DBNull.Value),
                                      new Param("@FreqType", SqlDbType.Int, frequencytype));

        }

        public static void Update (DateTime dateTime, Decimal? open, Decimal? high,
                           Decimal? low, Decimal? close, Decimal? volume, Decimal? price ,Int32 id)
        {
            SqlHelper.ExecuteNonQuery("UpdateQuote", 
                new Param("@DateTime", SqlDbType.DateTime, dateTime),
                new Param("@Open", SqlDbType.Money, open ?? (object)DBNull.Value),
                new Param("@High", SqlDbType.Money, high ?? (object)DBNull.Value),
                new Param("@Low", SqlDbType.Money, low ?? (object)DBNull.Value),
                new Param("@Close", SqlDbType.Money, close ?? (object)DBNull.Value),
                new Param("@Volume", SqlDbType.Int, volume),
                new Param("@Id", SqlDbType.Int, id),
                new Param("@Price", SqlDbType.Money, price ?? (object)DBNull.Value));
            
            
        }


        public static void Delete (Int32 id)
        {
            SqlHelper.ExecuteNonQuery("DeleteQuotesFromTool", new Param("@QuoteId", SqlDbType.Int, id));
        }
    }
   
}
